<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Staff\StaffAppletAction;
use App\Admin\Actions\Staff\StaffDataShareAction;
use App\Admin\Actions\Staff\StaffDeleteAction;
use App\Admin\Actions\Staff\StaffSwitchStatusAction;
use App\Admin\Grid\NGrid;
use App\Admin\Grid\Tools\BindStaffTools;
use App\Admin\Service\ShopUserService;
use App\Models\ShopUserModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Grid\Tools;

class StaffController extends AdminController
{
    protected $title = '员工管理';

    public function __construct(ShopUserService $service)
    {
        $this->service = $service;
    }

    public function grid(): NGrid
    {
        $user = ShopUserModel::query()->where(['user_id' => Admin::user()->user_id])->first();
        $grid = new NGrid(new ShopUserModel);
        $grid->model()->where(['user_type' => 3, 'user_delete_flag' => 2]);
//        if ($user->user_id !== 1) {
//            $mainId = $user->main_id !== 0 ?? $user->user_id;
//            $grid->model()->where('main_id', $mainId);
//        }
        $grid->model()->leftJoin('shop_images', 'img_id', 'user_shop_business_license_img_id')->select('*', 'shop_images.img_url');
        $grid->column('user_id', 'ID')->sortable();
        $grid->column('user_status', __('l.status'))->using([2 => '关闭', 1 => '打开']);
        $grid->column('user_type', __('staff.type'))->using(__('staff.typeArr'));
        $grid->column('user_sex', __('l.gender'))->using(__('staff.genderArr'));
        $grid->column('user_name', __('staff.user_name'));
        $grid->column('user_mobile', __('staff.user_mobile'));
        $grid->column('user_avatar', __('staff.user_avatar'))->display(function ($column) {
            if (empty($column)) return null;
            else return view('image', ['id' => $this->id, 'image' => fileIdToUrl($column)]);
        });

        $grid->column('user_mobile', __('staff.user_nickname'));
        $grid->column('user_shop_name', __('staff.user_shop_name'));
        $grid->column('user_shop_status', __('staff.user_shop_status'))->using(__('staff.shopStatusArr'));
        $grid->column('user_shop_business_license_img_id', __('staff.user_shop_business_license_img_id'))->display(function () {
            if (empty($his->img_url)) return null;
            else return view('image', ['id' => $this->id, 'image' => fileIdToUrl($his->img_url)]);
        });
        $grid->column('user_applet_order', __('staff.user_applet_order'))->using(__('staff.appletStatusArr'));
        $grid->column('user_peer_power', __('staff.user_peer_power'))->using(__('staff.powerStatusArr'));
        $grid->column('agent', __('staff.agent'))->using(__('staff.agentStatusArr'));
        $grid->column('invite_code', __('staff.invite_code'));
        $grid->column('is_system', __('staff.is_system'))->using(__('staff.isSystemStatusArr'));
        $grid->column('add_at', __('staff.created_at'));
        $grid->actions(function (Actions $action) {
            $action->disableView();
            $action->disableDelete();
            $action->disableEdit();
            //数据共享
            $action->add(new StaffDataShareAction((int)$action->row->user_peer_power));
            //代办
            $action->add(new StaffAppletAction((int)$action->row->user_applet_order));
            //切换状态
            $action->add(new StaffSwitchStatusAction((int)$action->row->user_status));
            //删除
            $action->add(new StaffDeleteAction);
        });
        $grid->disableCreateButton();
        $grid->disableActions(false);
        $grid->tools(function (Tools $tools) {
            $tools->append(new BindStaffTools);
        });
        Admin::js('js/staff.js?v' . random());
        return $grid;
    }
}
